package com.proxy.jdkproxy;

import com.proxy.target.Target;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Classname JdkProxy
 * @Description TODO
 * @Date 2021/8/24 0:08
 * @Created by 张斌
 */
public class JdkProxyFactory {

    private Target target;

    public JdkProxyFactory(Target target) {
        this.target = target;
    }

    public Object getProxyInstance(){
        Object proxyInstance = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("JdkProxyFactory start");
                Object ret = method.invoke(target, args);
                System.out.println("JdkProxyFactory end");
                return ret;
            }
        });
        return proxyInstance;
    }

}
